package www.bplead.com.controller.action.user;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import www.bplead.com.controller.action.BaseNetAction;
import www.bplead.com.controller.action.user.entity.UserInfo;
import www.bplead.com.controller.action.user.event.UserInfoPageRequest;
import www.bplead.com.controller.action.user.event.UserInfoPageResponse;
import www.bplead.com.controller.action.user.event.UserInfoRequest;
import www.bplead.com.controller.action.user.event.UserInfoResponse;
import www.bplead.com.controller.url.UserInfoUrlConstant;
import www.bplead.com.controller.util.StringUtil;

/**
 * Created by wangyangzi on 2016/9/4.
 */
public class UserAction extends BaseNetAction {

    private static final String TAG = "UserAction";
    private Context mContext ;

    @Inject
    public UserAction(Context context) {
        super(context);
        mContext = context;
    }

    @Subscribe
    public void getUserList(final UserInfoRequest request) {
        NtStringRequest stringRequest = new NtStringRequest(Request.Method.POST,UserInfoUrlConstant.getUserListUrl(),new Response.Listener<String>(){
            @Override
            public void onResponse(String response) {
                //打印请求返回结果
                Log.v(TAG, "getUserList onResponse : " + response);
                List<UserInfo> userList = StringUtil.getGson().fromJson(response,new TypeToken<List<UserInfo>>(){}.getType());

                UserInfoResponse userInfoResponse = new UserInfoResponse();
                userInfoResponse.userInfoList = userList;
                EventBus.getDefault().post(userInfoResponse);
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, "VolleyError:" + volleyError.toString());
                try {
                    byte[] htmlBodyBytes = volleyError.networkResponse.data;
                    Log.e(TAG, new String(htmlBodyBytes), volleyError);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                //post请求时提交参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("code", "01");

                //打印提交参数
                Log.v(TAG, "getUserList getParams : " + map.toString());

                return map;
            }
        };

        mQueue.add(stringRequest);
    }

    @Subscribe
    public void getUserListByPageSize(final UserInfoPageRequest request) {
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("code","03");
        hashMap.put("startPos",String.valueOf(request.startPos));
        hashMap.put("endPos",String.valueOf(request.endPos));

        String requestURL = StringUtil.makeGetURL(UserInfoUrlConstant.getUserListUrl(),hashMap);

        NtStringRequest stringRequest = new NtStringRequest(Request.Method.GET,requestURL,new Response.Listener<String>(){
            @Override
            public void onResponse(String response) {
                //打印请求返回结果
                Log.v(TAG, "getUserListByPageSize onResponse : " + response);
                List<UserInfo> userList = StringUtil.getGson().fromJson(response,new TypeToken<List<UserInfo>>(){}.getType());

                UserInfoPageResponse userInfoResponse = new UserInfoPageResponse();
                userInfoResponse.userInfoList = userList;
                EventBus.getDefault().post(userInfoResponse);
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, "VolleyError:" + volleyError.toString());
                try {
                    byte[] htmlBodyBytes = volleyError.networkResponse.data;
                    Log.e(TAG, new String(htmlBodyBytes), volleyError);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });

        mQueue.add(stringRequest);
    }
}
